package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealService setMealService;

    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal = setMealMapper.queryById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDish =  setMealDishMapper.queryBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    @ApiOperation("新增套餐")
    @Transactional
    @Override
    public void addMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.addMeal(setmeal);
        long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //批量插入
        setMealDishMapper.batchInsert(setmealDishes);

    }

    @Override
    public PageResult pagequery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page <SetmealVO> setmealVOS = setMealMapper.pagequery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(setmealVOS.getTotal(),setmealVOS);
        return pageResult;
    }

    @Transactional
    @Override
    public void updateMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.updateMeal(setmeal);
        //删除原有菜品
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //插入新菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setMealDishMapper.batchInsert(setmealDishes);
    }

    @Override
    public void updatestatus(Integer status, Long id) {
        setMealMapper.updatestatus(status,id);
    }


    @Transactional
    @Override
    public void deleteMeal(List<Long> ids) {
        setMealMapper.listdeleteMeal(ids);
        setMealDishMapper.listdeleteBySetmealId(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
